var uploader = {
    'data':{
        'token':'',
        'uploadDomain':'',
        'domain':''
    },
    'complete':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic-pic").attr("src",url);
        $(".hidden-pic").val(res.key);
    },


    'error':function () {
        console.dir(arguments);
    },
    // 分片上传时的进度展示功能在此书写.
    'next':function (response) {
        // var chunks = response.chunks || [];
        // var total = response.total;
    },
    'init':function(){
        $.ajax({
            'url':'/upload/get-token',
            'type':'POST',
            'dataType':'json',
            'success':function(data){
                if(data.code != 200)
                {
                    layer.alert(data.msg);
                    return false;
                }
                uploader.data.token = data.data.token;      // token
                uploader.data.domain = data.data.domain;    // 显示域名
                // uploader.data.uploadDomain = 'idv093d.qiniudns.com';    // 上传域名
                // console.log(uploader.data.token)
            }
        });
    },
    'eventBind':function(){
        this.init();
        $('.default').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
    }
};
$(function () {
    uploader.eventBind();
})
$('#qued').click(function () {
    //表单序列化
    var type = $('input[name="type"]:checked ').val();
    var img_url = $('.hidden-pic').val();
    var oid = $('.oid').val();
    flag = true;
    if(img_url == ""){
        layer.msg("请上传图片");
        flag=false;
    }else if(type == ""){
        layer.msg("请选择类型");
        flag=false;
    }else if(oid==""){
        layer.msg("请输入对象ID");
        flag=false;
    }
    if(flag) {
        $.ajax({
            'type': 'POST',
            'url': '/banner/banner-add',
            'dataType': 'json',
            'async': false,
            'data': {'img_url': img_url, 'type': type, 'oid': oid},
            success: function (data) {
                //    alert("Data Saved:111 " );
                if (data.code == '-1') {
                    layer.msg("未添加成功，请重试")
                } else if (data.code == '200') {
                    layer.msg(data.msg)
                    location.href = "/banner/banner-index"
                }

            },
            error: function () {
                alert("添加失败，请重新添加 ");
            },
        });
    }
})